<template>
  <div class="report-container">
    <el-card shadow="never">
      <!-- 搜索栏 -->
      <div class="filter-header">
        <el-input
          v-model="filter.id"
          placeholder="编号"
          style="width: 120px"
          class="filter-item"
          clearable
          @keyup.enter="handleSearch"
        />
        <el-input
          v-model="filter.category"
          placeholder="类别"
          style="width: 120px; margin-left: 10px"
          class="filter-item"
          clearable
          @keyup.enter="handleSearch"
        />
        <el-button 
          type="primary" 
          style="margin-left: 10px"
          @click="handleSearch"
        >
          <el-icon><search /></el-icon> 搜索
        </el-button>
        <el-button 
          style="margin-left: 10px"
          @click="resetSearch"
        >
          <el-icon><refresh /></el-icon> 重置
        </el-button>
      </div>

      <!-- 数据表格 -->
      <el-table
        :data="filteredData"
        style="width: 100%"
        header-row-class-name="table-header"
        stripe
        border
      >
        <el-table-column prop="id" label="编号" width="80" align="center"/>
        <el-table-column prop="category" label="类别" width="180"/>
        <el-table-column prop="requirement" label="填写要求" width="120"/>
        <el-table-column prop="deadline" label="截至时间" width="120" align="center"/>
        <el-table-column prop="content" label="周报内容" width="120">
          <template #default="scope">
            {{ scope.row.content || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="score" label="分数" width="100" align="center"/>
        <el-table-column prop="actualScore" label="实际得分" width="100" align="center">
          <template #default="scope">
            {{ scope.row.actualScore || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="comment" label="教师评语" width="120">
          <template #default="scope">
            {{ scope.row.comment || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template #default="scope">
            <div style="display: inline-flex;">
              <el-button 
                type="primary" 
                size="small"
                @click="handleEdit(scope.row)"
                style="margin-right: 8px;"
              >
                <el-icon><edit /></el-icon> 修改
              </el-button>
              <el-button 
                type="danger" 
                size="small"
                @click="handleDelete(scope.row)"
              >
                <el-icon><delete /></el-icon> 删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页信息 -->
      <div class="pagination-wrapper">
        <span class="pagination-text">第1到{{ filteredData.length }}条，共{{ filteredData.length }}条记录。</span>
        <div class="page-size">
          <span>10条记录每页</span>
          <el-icon><arrow-down /></el-icon>
        </div>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog 
      :title="'编辑周报 - ' + form.category"
      v-model="editDialogVisible"
      width="50%"
      @closed="handleDialogClosed"
    >
      <el-form 
        :model="form" 
        :rules="rules" 
        ref="formRef" 
        label-width="120px"
      >
        <el-form-item label="编号" prop="id">
          <el-input v-model="form.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入类别"></el-input>
        </el-form-item>
        <el-form-item label="填写要求" prop="requirement">
          <el-input 
            type="textarea"
            :rows="3"
            v-model="form.requirement" 
            placeholder="请输入填写要求"
          ></el-input>
        </el-form-item>
        <el-form-item label="截至时间" prop="deadline">
          <el-date-picker
            v-model="form.deadline"
            type="datetime"
            placeholder="选择截至时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="周报内容" prop="content">
          <el-input 
            type="textarea"
            :rows="4"
            v-model="form.content" 
            placeholder="请输入周报内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input-number 
            v-model="form.score" 
            :min="0" 
            :max="100" 
            :precision="2" 
            controls-position="right"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="实际得分" prop="actualScore">
          <el-input-number 
            v-model="form.actualScore" 
            :min="0" 
            :max="100" 
            :precision="2" 
            controls-position="right"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="教师评语" prop="comment">
          <el-input 
            type="textarea"
            :rows="3"
            v-model="form.comment" 
            placeholder="请输入教师评语"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Search,
  Refresh,
  Edit,
  Delete,
  ArrowDown
} from '@element-plus/icons-vue'

export default {
  setup() {
    const filter = ref({
      id: '',
      category: '',
      search: ''
    })

    const tableData = ref([
      {id:11, category:'第1次周报', requirement:'111111111', deadline:'2020-07-24', content:'-', score:25.00, actualScore:null, comment:'-', action:21},
      {id:11, category:'第2次周报', requirement:'111111111', deadline:'2020-08-02', content:'-', score:25.00, actualScore:null, comment:'-', action:21},
      {id:11, category:'第3次周报', requirement:'111111111', deadline:'2020-08-11', content:'-', score:25.00, actualScore:null, comment:'-', action:21},
      {id:11, category:'第4次周报', requirement:'111111111', deadline:'2020-08-20', content:'-', score:25.00, actualScore:null, comment:'-', action:21},
      {id:11, category:'pyfron程序设计基础总结', requirement:'111111111', deadline:'2020-08-20', content:'-', score:100, actualScore:null, comment:'-', action:21},
      {id:11, category:'C语言程序设计基础总结', requirement:'111111111', deadline:'2020-08-20', content:'-', score:100, actualScore:null, comment:'-', action:21},
      {id:12, category:'第1次周报', requirement:'11', deadline:'2020-07-11', content:'123', score:25.00, actualScore:20, comment:'1', action:21},
      {id:12, category:'第2次周报', requirement:'11', deadline:'2020-07-21', content:'-', score:25.00, actualScore:null, comment:'-', action:21},
      {id:12, category:'第3次周报', requirement:'11', deadline:'2020-07-31', content:'-', score:25.00, actualScore:null, comment:'-', action:21},
      {id:12, category:'第4次周报', requirement:'11', deadline:'2020-08-10', content:'-', score:25.00, actualScore:null, comment:'-', action:21}
    ])

    const editDialogVisible = ref(false)
    const form = ref({})
    const formRef = ref(null)

    const rules = {
      category: [
        { required: true, message: '请输入类别', trigger: 'blur' }
      ],
      requirement: [
        { required: true, message: '请输入填写要求', trigger: 'blur' }
      ],
      deadline: [
        { required: true, message: '请选择截至时间', trigger: 'change' }
      ],
      score: [
        { required: true, message: '请输入分数', trigger: 'blur' }
      ]
    }

    const filteredData = computed(() => {
      return tableData.value.filter(item => {
        return (
          (filter.value.id === '' || item.id.toString().includes(filter.value.id)) &&
          (filter.value.category === '' || item.category.includes(filter.value.category))
        )
      })
    })

    const handleSearch = () => {
      ElMessage({
        message: '搜索完成',
        type: 'success',
        duration: 1000
      })
    }

    const resetSearch = () => {
      filter.value = {
        id: '',
        category: '',
        search: ''
      }
      ElMessage({
        message: '已重置搜索条件',
        type: 'info',
        duration: 1000
      })
    }

    const handleEdit = (row) => {
      form.value = JSON.parse(JSON.stringify(row))
      editDialogVisible.value = true
    }

    const handleDelete = (row) => {
      ElMessageBox.confirm('确定要删除这条周报记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = tableData.value.findIndex(item => item.id === row.id && item.category === row.category)
        if (index !== -1) {
          tableData.value.splice(index, 1)
          ElMessage.success('删除成功')
        }
      }).catch(() => {
        ElMessage.info('已取消删除')
      })
    }

    const handleDialogClosed = () => {
      formRef.value.resetFields()
    }

    const submitForm = () => {
      formRef.value.validate(valid => {
        if (valid) {
          const index = tableData.value.findIndex(item => item.id === form.value.id && item.category === form.value.category)
          if (index !== -1) {
            tableData.value.splice(index, 1, { ...form.value })
            ElMessage.success('修改成功')
            editDialogVisible.value = false
          }
        }
      })
    }

    return {
      filter,
      tableData,
      editDialogVisible,
      form,
      formRef,
      rules,
      filteredData,
      handleSearch,
      resetSearch,
      handleEdit,
      handleDelete,
      handleDialogClosed,
      submitForm
    }
  }
}
</script>

<style scoped>
.report-container {
  padding: 20px;
}

.filter-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.filter-item {
  margin-left: 10px;
}

.table-header th {
  background-color: #f5f7fa !important;
  font-weight: 600;
}

.pagination-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
  padding: 10px 20px;
  background: #f5f7fa;
  border-radius: 4px;
}

.page-size {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.page-size span {
  margin-right: 5px;
  color: #606266;
}

.el-button [class*="el-icon-"] {
  margin-right: 5px;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-date-editor, .el-input, .el-textarea {
  width: 100%;
}

.el-table {
  margin-top: 20px;
}
</style>